package jgl.vajra.driver.part.home.fragment;

import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.view.OptionsPickerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import jgl.vajra.driver.R;
import jgl.vajra.driver.bean.AddressBean;
import jgl.vajra.driver.bean.HomeMechanicsBean;
import jgl.vajra.driver.bean.HomePlaceBean;
import jgl.vajra.driver.databinding.FragmentHomePageBinding;
import jgl.vajra.driver.event.WorkplaceEvent;
import jgl.vajra.driver.manager.FragmentManager;
import jgl.vajra.driver.manager.IntentManager;
import jgl.vajra.driver.part.home.contract.HomePageContract;
import jgl.vajra.driver.part.home.pop.AccountingMethodPop;
import jgl.vajra.driver.part.home.pop.AllMachineryPop;
import jgl.vajra.driver.part.home.viewModel.HomePageViewModel;
import jgl.vajra.driver.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.driver.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.driver.widget.manager.SPManager;
import jgl.vajra.driver.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.driver.widget.mvvm.view.BaseMVVMFragment;
import jgl.vajra.driver.widget.utils.ToastUtils;
import jgl.vajra.driver.widget.utils.UIUtils;

@CreateViewModel(HomePageViewModel.class)
public class HomePageFragment extends BaseMVVMFragment<HomePageViewModel, FragmentHomePageBinding> implements HomePageContract.View, BaseBindingItemPresenter {

    private SingleTypeBindingAdapter adapterMechanics;
    private SingleTypeBindingAdapter adapterPlace;
    private List<HomeMechanicsBean> listMechanics = new ArrayList<>();
    private List<HomePlaceBean> listPlace = new ArrayList<>();

    private AllMachineryPop allMachineryPop;
    private AccountingMethodPop accountingMethodPop;
    private OptionsPickerView pvNoLinkOptions;
    private List<Fragment> fragmentList;
    private boolean isCityPicker;
    private boolean isSettleAccounts;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home_page;
    }

    @Override
    public void initView() {
        mBinding.setView(this);
        //跑马灯效果
        mBinding.tvTitle.setSelected(true);

        mBinding.tvOriginalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线

        //位置信息
        AddressBean addressData = SPManager.HomeData.getAddressData();

        if (addressData != null && !TextUtils.isEmpty(addressData.locality)) {
            mBinding.tvAddress.setText(addressData.locality.replace("市", ""));
        }

        listMechanics.addAll(HomeMechanicsBean.getListData());

        //机械类型
        // 不可嵌套滑动
        mBinding.recyclerViewMechanics.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerViewMechanics.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerViewMechanics.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        adapterMechanics = new SingleTypeBindingAdapter(mActivity, listMechanics, R.layout.item_home_page_mechanics);
        mBinding.recyclerViewMechanics.setAdapter(adapterMechanics);
        adapterMechanics.setItemPresenter(this);

        List<String> titleList = new ArrayList<>();
        titleList.addAll(setTitleList("挖掘机"));
        List<Fragment> fragmentList = new ArrayList<>();
        for (int i = 0; i < titleList.size(); i++) {
            HomeVehicleTypeFragment listFragment = FragmentManager.getHomeVehicleTypeFragment(i, "挖掘机", titleList.get(i), true);
            fragmentList.add(listFragment);
        }
        mBinding.tabLayout.setCustomViewTabLayoutView(getChildFragmentManager(), titleList, fragmentList);

        //新增地址
        // 不可嵌套滑动
        mBinding.recyclerViewPlace.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerViewPlace.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerViewPlace.setLayoutManager(new LinearLayoutManager(getContext()));
        adapterPlace = new SingleTypeBindingAdapter(mActivity, listPlace, R.layout.item_home_page_place);
        mBinding.recyclerViewPlace.setAdapter(adapterPlace);
        adapterPlace.setItemPresenter(this);
    }


    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }


    @Override
    public void onItemClick(int position, Object itemData) {

    }

    //位置
    @Override
    public void onTriangle() {
        IntentManager.toCityChoiceActivity(mActivity);
    }

    //礼盒
    @Override
    public void onGiftBox() {
        //订单
     //   IntentManager.toOrderStatusActivity(mActivity);

        IntentManager.toOrderEvaluateActivity(mActivity);
    }

    //全部机械
    @Override
    public void onAllMachinery() {
        if (allMachineryPop == null)
            allMachineryPop = new AllMachineryPop(mActivity);
        allMachineryPop.setAllMachineryListener((itemData, name) -> {
            if (listMechanics.get(itemData).isClick) {
                return;
            }
            for (int i = 0; i < HomeMechanicsBean.getListData().size(); i++) {
                listMechanics.get(i).isClick = false;
            }

            listMechanics.get(itemData).isClick = true;
            adapterMechanics.refresh();

            ViewGroup.LayoutParams params = mBinding.rlAllVehicle.getLayoutParams();
            if (name.equals("挖掘机")) {
                params.height = (int) UIUtils.getResource().getDimension(R.dimen.dp_240);
            } else {
                params.height = (int) UIUtils.getResource().getDimension(R.dimen.dp_200);
            }
            mBinding.rlAllVehicle.setLayoutParams(params);
            mBinding.tabLayout.getTabLayout().removeAllTabs();
            //清空数据
            //  CommonUtils.clearFragmentCache(getChildFragmentManager(), (FragmentPagerAdapter) mBinding.tabLayout.getViewPager().getAdapter());

            List<String> titleList = new ArrayList<>();

            titleList.addAll(setTitleList(name));

            fragmentList = new ArrayList<>();
            for (int i = 0; i < titleList.size(); i++) {
                HomeVehicleTypeFragment listFragment = FragmentManager.getHomeVehicleTypeFragment(i, name, titleList.get(i), true);
                fragmentList.add(listFragment);
            }//这里为什么不走?
            mBinding.tabLayout.setCustomViewTabLayoutView(getChildFragmentManager(), titleList, fragmentList);
        });
        allMachineryPop.showPopupWindow(mBinding.rlHead);
    }

    //全部车型
    @Override
    public void onAllVehicle() {
        if (allMachineryPop == null)
            allMachineryPop = new AllMachineryPop(mActivity);
        allMachineryPop.showPopupWindow(mBinding.tvAllMachinery);
    }

    //台班算账
    @Override
    public void onShiftAccounting() {
        mBinding.tvMileageAccounting.setBackgroundResource(R.drawable.shape_color_c4c8de_10dp_right);
        mBinding.tvShiftAccounting.setBackgroundResource(R.drawable.shape_while_10dp_top_left);
        mBinding.ivAccountingBg.setImageResource(R.drawable.ic_shift_accounting);
        mBinding.tvMileageAccounting.setTextColor(UIUtils.getColor(R.color.white));
        mBinding.tvShiftAccounting.setTextColor(UIUtils.getColor(R.color.color_333333));

        mBinding.etAccounting.setHint("选择干活时长(台班)");
    }

    //里程算账
    @Override
    public void onMileageAccounting() {
        mBinding.ivAccountingBg.setImageResource(R.drawable.ic_mileage_accounting);
        mBinding.tvMileageAccounting.setBackgroundResource(R.drawable.shape_while_10dp_top_right);
        mBinding.tvShiftAccounting.setBackgroundResource(R.drawable.shape_color_c4c8de_10dp_left);
        mBinding.tvShiftAccounting.setTextColor(UIUtils.getColor(R.color.white));
        mBinding.tvMileageAccounting.setTextColor(UIUtils.getColor(R.color.color_333333));

        mBinding.etAccounting.setHint("选择里程算账(一趟)");
    }

    //干活地
    @Override
    public void onWorkingField() {
        IntentManager.toWorkingFieldActivity(mActivity);
    }

    @Override
    protected boolean isRegisterEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCityPickerEvent(WorkplaceEvent event) {
        mBinding.tvSingleWorkingField.setText(event.bean.detailedAddress + "(" + event.bean.address + ")");

        isCityPicker = true;
        if (isSettleAccounts) {
            mBinding.llBudget.setVisibility(View.VISIBLE);
        }
    }


    //干活的地址选择
    @Override
    public void onCommonAddress() {
        IntentManager.toCommonAddressActivity(mActivity);
    }

    //增加吊货地
    @Override
    public void onAddCargoPlace() {
        if (listPlace.size() >= 7) {
            ToastUtils.showShort("吊货地不得超过9个");
            return;
        }
        listPlace.add(new HomePlaceBean(true));
        adapterPlace.refresh();
    }

    //增加卸货地
    @Override
    public void onAddDischargePlace() {
        if (listPlace.size() >= 7) {
            ToastUtils.showShort("卸货地不得超过9个");
            return;
        }
        listPlace.add(new HomePlaceBean(false));
        adapterPlace.refresh();
    }

    //算账方式
    @Override
    public void onSettleAccounts() {
        if (accountingMethodPop == null)
            accountingMethodPop = new AccountingMethodPop(mActivity);
        accountingMethodPop.setAccountingMethodListener(itemData -> {
            mBinding.tvSettleAccounts.setText(itemData);
            isSettleAccounts = true;
            if (isCityPicker) {
                mBinding.llBudget.setVisibility(View.VISIBLE);
            }

        });
        accountingMethodPop.showPopupWindow(mBinding.tvAllMachinery);
    }

    //立即用车
    @Override
    public void onImmediatelyCar() {
        //确认单子
        IntentManager.toConfirmOrderActivity(mActivity);
    }

    //预约用车
    @Override
    public void onReservedCar() {
        List<String> dayList = new ArrayList<>();
        dayList.add("今天");
        dayList.add("明天");
        dayList.add("后天");
        List<String> hourList = new ArrayList<>();
        hourList.add("00点");
        hourList.add("01点");
        hourList.add("02点");
        hourList.add("03点");
        hourList.add("04点");
        hourList.add("05点");
        hourList.add("06点");
        hourList.add("07点");
        hourList.add("08点");
        hourList.add("09点");
        hourList.add("10点");
        hourList.add("11点");
        hourList.add("12点");
        hourList.add("13点");
        hourList.add("14点");
        hourList.add("15点");
        hourList.add("16点");
        hourList.add("17点");
        hourList.add("18点");
        hourList.add("19点");
        hourList.add("20点");
        hourList.add("21点");
        hourList.add("22点");
        hourList.add("23点");
        List<String> minuteList = new ArrayList<>();
        minuteList.add("00分");
        minuteList.add("10分");
        minuteList.add("20分");
        minuteList.add("30分");
        minuteList.add("40分");
        minuteList.add("50分");
        pvNoLinkOptions = new OptionsPickerBuilder(mActivity, (options1, options2, options3, v) -> {
            String str = "food:" + dayList.get(options1)
                    + "\nclothes:" + hourList.get(options2)
                    + "\ncomputer:" + minuteList.get(options3);
            ToastUtils.showShort(str);
        })
                .setLayoutRes(R.layout.pickerview_custom_condition, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final TextView tvDetermine = (TextView) v.findViewById(R.id.tv_determine);
                        ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                        tvDetermine.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvNoLinkOptions.returnData();
                                pvNoLinkOptions.dismiss();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvNoLinkOptions.dismiss();
                            }
                        });
                    }
                })

                .setLineSpacingMultiplier(3f)//行间距
                .setItemVisibleCount(5)
                .setContentTextSize(20)
                .setTextColorCenter(Color.BLACK)
                .build();
        pvNoLinkOptions.setNPicker(dayList, hourList, minuteList);
        pvNoLinkOptions.setSelectOptions(1, 11, 3);
        pvNoLinkOptions.show();
    }

    //算算账
    @Override
    public void onCalculateAccounts() {
        IntentManager.toCalculateAccountsActivity(mActivity);
    }

    //删除地点
    @Override
    public void onDeletePlace(int position) {
        listPlace.remove(position);
        adapterPlace.refresh();
    }

    //点击机械
    @Override
    public void onClickMechanics(int position, HomeMechanicsBean itemData) {
        if (itemData.isClick)
            return;

        for (int i = 0; i < HomeMechanicsBean.getListData().size(); i++) {
            listMechanics.get(i).isClick = false;
        }
        listMechanics.get(position).isClick = true;
        adapterMechanics.refresh();

        ViewGroup.LayoutParams params = mBinding.rlAllVehicle.getLayoutParams();
        if (itemData.name.equals("挖掘机")) {
            params.height = (int) UIUtils.getResource().getDimension(R.dimen.dp_240);
        } else {
            params.height = (int) UIUtils.getResource().getDimension(R.dimen.dp_200);
        }
        mBinding.rlAllVehicle.setLayoutParams(params);

        mBinding.tabLayout.getTabLayout().removeAllTabs();
        List<String> titleList = new ArrayList<>();

        titleList.addAll(setTitleList(itemData.name));

        fragmentList = new ArrayList<>();
        for (int i = 0; i < titleList.size(); i++) {
            HomeVehicleTypeFragment listFragment = FragmentManager.getHomeVehicleTypeFragment(i, itemData.name, titleList.get(i), true);
            fragmentList.add(listFragment);
        }
        mBinding.tabLayout.setCustomViewTabLayoutView(getChildFragmentManager(), titleList, fragmentList);
    }

    public List<String> setTitleList(String name) {
        if (name.equals("随车吊") || name.equals("低空炮")) {
            mBinding.llSingleWorkingField.setVisibility(View.GONE);
            mBinding.llMultipleWorkingField.setVisibility(View.VISIBLE);
        } else {
            mBinding.llSingleWorkingField.setVisibility(View.VISIBLE);
            mBinding.llMultipleWorkingField.setVisibility(View.GONE);
        }

        // EventBus.getDefault().post(new HomePageTypeEvent(name));
        List<String> list = new ArrayList<>();
        if (name.equals("挖掘机")) {
            list.add("40");
            list.add("50");
            list.add("60");
            list.add("200");
            list.add("260 ");
        } else if (name.equals("随车吊")) {
            list.add("16吨");
            list.add("25吨");
            list.add("35吨");
            list.add("50吨");
            list.add("80吨 ");
        } else if (name.equals("低空炮")) {
            list.add("50吨");
            list.add("60吨");
            list.add("80吨");
            list.add("100吨 ");
        } else if (name.equals("专吊")) {
            list.add("25吨");
            list.add("35吨");
            list.add("40吨");
            list.add("50吨");
            list.add("90吨  ");
        } else if (name.equals("高空车")) {
            list.add("17米");
            list.add("21米");
            list.add("28米  ");
        } else if (name.equals("叉车")) {
            list.add("3吨");
            list.add("3.5吨");
            list.add("小4吨");
            list.add("小5吨");
            list.add("大5吨  ");
        }
        return list;
    }
}
